home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / ARexx / mouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-17  |  4.9 KB  |  244 lines

  1. /*
  2.  * mouse.c v1.0 by Chris Ludwig
  3.  */
  4.  
  5. #include    <exec/types.h>
  6.  
  7. #include    <libraries/dos.h>
  8. #include    <libraries/dosextens.h>
  9.  
  10. #include    <devices/input.h>
  11. #include    <devices/inputevent.h>
  12.  
  13. #include    <clib/rexxsyslib_protos.h>
  14. #include    <clib/alib_protos.h>
  15. #include    <clib/exec_protos.h>
  16. #include    <clib/dos_protos.h>
  17.  
  18. #include    <rexx/storage.h>
  19. #include    <rexx/rxslib.h>
  20.  
  21. #include    <stdio.h>
  22. #include    <string.h>
  23.  
  24. #include    "SimpleRexx.h"
  25.  
  26.  
  27. /* input.device stuff */
  28. struct MsgPort *inputdevport;
  29. struct InputEvent phony;
  30. struct IOStdReq *inputrequest;
  31. BYTE inputopenerror;
  32.  
  33. AREXXCONTEXT    RexxStuff;
  34.  
  35. void Quit(char whytext[],UBYTE level)
  36. {
  37.     if (!inputopenerror) CloseDevice((struct IORequest *) inputrequest);
  38.  
  39.     if (inputrequest) DeleteStdIO(inputrequest);
  40.  
  41.     if (inputdevport) DeletePort(inputdevport);
  42.  
  43.     FreeARexx(RexxStuff);
  44.  
  45.     printf("%s\n",whytext);
  46.     Exit(level);
  47. }
  48.  
  49.  
  50. /*
  51.  * Lattice control-c stop...
  52.  */
  53. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  54. int chkabort(void) { return(0); }  /* really */
  55.  
  56.  
  57. void click(UWORD code)
  58. {
  59. printf("in click()\n");
  60.  
  61.     phony.ie_NextEvent = NULL;
  62.     phony.ie_Class = IECLASS_RAWMOUSE;
  63.     phony.ie_TimeStamp.tv_secs = 0;
  64.     phony.ie_TimeStamp.tv_micro = 0;
  65.     phony.ie_Code = code;                    /* button down */
  66.     phony.ie_Qualifier = 0;
  67.     phony.ie_X = 0;
  68.     phony.ie_Y = 0;
  69.  
  70.     inputrequest -> io_Command = IND_WRITEEVENT;
  71.     inputrequest -> io_Flags = 0;
  72.     inputrequest -> io_Length = sizeof(struct InputEvent);
  73.     inputrequest -> io_Data = (APTR)&phony;
  74.  
  75.     DoIO(inputrequest);
  76.  
  77.  
  78.     phony.ie_NextEvent = NULL;
  79.     phony.ie_Class = IECLASS_RAWMOUSE;
  80.     phony.ie_TimeStamp.tv_secs = 0;
  81.     phony.ie_TimeStamp.tv_micro = 0;
  82.     phony.ie_Code = code | IECODE_UP_PREFIX;    /* button up */
  83.     phony.ie_Qualifier = 0;
  84.     phony.ie_X = 0;
  85.     phony.ie_Y = 0;
  86.  
  87.     inputrequest -> io_Command = IND_WRITEEVENT;
  88.     inputrequest -> io_Flags = 0;
  89.     inputrequest -> io_Length = sizeof(struct InputEvent);
  90.     inputrequest -> io_Data = (APTR)&phony;
  91.  
  92.     DoIO(inputrequest);
  93. }
  94.  
  95.  
  96. void move(LONG mousex, LONG mousey)
  97. {
  98.     /* send phony mouse message to input stream */
  99.  
  100.     phony.ie_NextEvent = NULL;
  101.     phony.ie_Class = IECLASS_POINTERPOS;
  102.     phony.ie_TimeStamp.tv_secs = 0;
  103.     phony.ie_TimeStamp.tv_micro = 0;
  104.     phony.ie_Code = 0;
  105.     phony.ie_Qualifier = 0;
  106.     phony.ie_X = mousex;
  107.     phony.ie_Y = mousey;
  108.  
  109.     inputrequest -> io_Command = IND_WRITEEVENT;
  110.     inputrequest -> io_Flags = 0;
  111.     inputrequest -> io_Length = sizeof(struct InputEvent);
  112.     inputrequest -> io_Data = (APTR)&phony;
  113.  
  114.     DoIO(inputrequest);
  115. }
  116.  
  117.  
  118. /*
  119.  * A *VERY* simple and simple-minded example of using the SimpleRexx.c code.
  120.  *
  121.  *
  122.  * Note: You will want to RUN this program or have another shell available such
  123.  *       that you can still have access to ARexx...
  124.  */
  125. void main(int argc,char *argv[])
  126. {
  127. short    loopflag=TRUE;
  128. ULONG    signals;
  129.  
  130. int x, y, length;
  131.  
  132.     if ((inputdevport=CreatePort(0,0)) == NULL)
  133.         Quit("Couldn't create a port for input.device",25);
  134.  
  135.  
  136.     if ((inputrequest=CreateStdIO(inputdevport)) == NULL)
  137.         Quit("Couldn't create request block for input device",25);
  138.  
  139.  
  140.     if ((inputopenerror=OpenDevice("input.device",0,inputrequest,0)) != 0)
  141.         Quit("Couldn't open input.device",25);
  142.  
  143.     /*
  144.      * Note that SimpleRexx is set up such that you do not
  145.      * need to check for an error to initialize your REXX port
  146.      * This is so your application could run without REXX...
  147.      */
  148.     RexxStuff=InitARexx("mouse","mouse");
  149.  
  150.     if (argc)
  151.     {
  152.         if (RexxStuff) printf("Send commands to port %s\n",ARexxName(RexxStuff));
  153.         else printf("ARexx is not available\n");
  154.     }
  155.  
  156.     while (loopflag)
  157.     {
  158.         signals=ARexxSignal(RexxStuff);
  159.  
  160.         if (signals)
  161.         {
  162.             struct    RexxMsg        *rmsg;
  163.  
  164.             signals=Wait(signals);
  165.  
  166.             /*
  167.              * Process the ARexx messages...
  168.              */
  169.             while (rmsg=GetARexxMsg(RexxStuff))
  170.             {
  171.             char    cBuf[24];
  172.             char    *nextchar;
  173.             char    *error=NULL;
  174.             char    *result=NULL;
  175.             long    errlevel=0;
  176.  
  177.                 nextchar=stptok(ARG0(rmsg),cBuf,24," ,");
  178.                 if (*nextchar) nextchar++;
  179.  
  180.                 if (!stricmp("CLICK",cBuf))
  181.                 {
  182.                     nextchar=stptok(nextchar,cBuf,24," ,");
  183.                     if (*nextchar) nextchar++;
  184.  
  185.                     if (!stricmp("LEFT",cBuf))
  186.                     {
  187.                         click(IECODE_LBUTTON);
  188.                     }
  189.                     else if (!stricmp("MIDDLE",cBuf))
  190.                     {
  191.                         click(IECODE_MBUTTON);
  192.                     }
  193.                     else if (!stricmp("RIGHT",cBuf))
  194.                     {
  195.                         click(IECODE_RBUTTON);
  196.                     }
  197.                     else
  198.                     {
  199.                         /* Default to left button */
  200.                         click(IECODE_LBUTTON);
  201.                     }
  202.                 }
  203.                 else if (!stricmp("MOVE",cBuf))
  204.                 {
  205.                     nextchar=stptok(nextchar,cBuf,24," ,");
  206.                     if (*nextchar) nextchar++;
  207.  
  208.                     length=stcd_i(cBuf,&x);
  209.  
  210.                     nextchar=stptok(nextchar,cBuf,24," ,");
  211.                     if (*nextchar) nextchar++;
  212.  
  213.                     length=stcd_i(cBuf,&y);
  214.  
  215.  
  216.                     move((LONG) x,(LONG) y);
  217.                 }
  218.                 else if (!stricmp("VERSION",cBuf))
  219.                 {
  220.                     result="mouse v1.0";
  221.                 }
  222.                 else if (!stricmp("QUIT",cBuf))
  223.                 {
  224.                     loopflag=FALSE;
  225.                 }
  226.                 else
  227.                 {
  228.                     error="Unknown command";
  229.                     errlevel=20;
  230.                 }
  231.  
  232.                 if (error)
  233.                 {
  234.                     SetARexxLastError(RexxStuff,rmsg,error);
  235.                 }
  236.                 ReplyARexxMsg(RexxStuff,rmsg,result,errlevel);
  237.             }
  238.  
  239.         }
  240.         else loopflag=FALSE;
  241.     }
  242.     Quit("QUIT command received.\n",0);
  243. }
  244.